<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main {
        	padding-top: 6px;
        }
        .main ul li {
        	background-color: #fff;
			color: #333;
			font-size: 15px;
			border-bottom: 1px solid #e8e8e8;
			padding: 15px;
        }
        .main ul li:last-child {
        	border-bottom: none;
        }
        .main ul li .li_box {
			display: -webkit-box;
			display: -webkit-flex;
        }
        .main ul li .li_box .li_boxl {
        	width: 0;
			flex: 1;
			-webkit-flex: 1;
			box-flex: 1;
			-webkit-box-flex: 1;
        }
        .main ul li .li_box .li_boxr {
        	display: -webkit-box;
        	-webkit-box-align: center;
        }
        .main ul li .li_box .li_boxr input {
        	display: none;
        }
        .main ul li .li_box .li_boxr .check_icon {
        	width: 20px;
        	display: none;
        }
        .without_data{
            display: none;
            color:#ccc;
            text-align:center;
            padding-top: 75%;
        }
    </style>
</head>
<body>
    <div class="main">
        <p class="without_data">没有分店</p>

    	<ul id="data_ul">
<!--     		<li tapmode onclick="fnClick(this);">
    			<div class="li_box">
    				<div class="li_boxl">分店名称</div>
    				<div class="li_boxr">
		    			<input type="checkbox" name="branch">
		    			<img class="check_icon" src="../image/branch_check_icon.png">
    				</div>
    			</div>
    		</li> -->

    	</ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="dataTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li tapmode onclick="fnClick(this);">
            <div class="li_box">
                <div class="li_boxl">{{=it[i]['name']}}</div>
                <div class="li_boxr">
                    <input type="checkbox" name="branch" value="{{=it[i]['id']}}">
                    <img class="check_icon" src="../image/branch_check_icon.png">
                </div>
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        shop = $api.getStorage('shop_data');
        fnGetBranch();
        api.addEventListener({
            name: 'choseAllBranchEvent'
        }, function(ret, err) {
            if (ret) {
                var li_count = $('#data_ul li').length;
                var check_count = $('input[name=branch]:checked').length;
                if(li_count == check_count) {
                    $('input[name=branch]').prop('checked',false);
                    $('.check_icon').css('display','none');
                }else {
                    $('input[name=branch]').prop('checked',true);
                    $('.check_icon').css('display','block');
                }
            }
        });

        api.addEventListener({
            name: 'selectBranchEvent'
        }, function(ret, err) {
            if (ret) {
                branch_ids = []; 
                $('input[name=branch]:checked').each(function(i,v){
                    var branch_id = $(this).val();
                    branch_ids.push(branch_id);
                })
                api.sendEvent({
                    name: 'subShopIdsEvent',
                    extra: {
                        branch_ids: branch_ids, 
                    }
                });
                api.closeWin();
            }
        });
    };

    function fnClick(this_) {
    	if ($(this_).find('input[name=branch]').is(':checked')) {
    		$(this_).find('input[name=branch]').prop('checked', false);
    		$(this_).find('.check_icon').css('display', 'none');
    	} else {
    		$(this_).find('input[name=branch]').prop('checked', true);
    		$(this_).find('.check_icon').css('display', 'block');
    	}
    }

    function fnGetBranch() {
        api.ajax({
            url: rootPath+'/api/seller/get_branch',
            method: 'get',
            data: {
                values: { 
                    shop_id: shop.id,
                    token: shop.token
                },

            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200) {
                    setDotData(ret.data,'dataTpl','data_ul',true);
                }else {
                    $('.without_data').show();
                }
            } else {
                alert(JSON.stringify(err.msg));
            }
        });
    }

</script>
</html>